<template>
  <div id="box">
    <h2 style="margin-bottom: 10px;color: black;">在线课程论坛</h2>
    <el-form :model="numberValidateForm" ref="numberValidateForm" label-width="100px" class="registForm">
      <h3 style="margin-bottom: 30px;color:aqua">密码找回</h3>
      <el-form-item label="输入邮箱" prop="email">
        <el-input type="email" placeholder="请输入您的邮箱" v-model="numberValidateForm.email"></el-input>
      </el-form-item>
      <el-form-item label="邮箱码" prop="checkCode">
        <el-input style="width: 200px;float: left;" placeholder="请输入邮箱验证码" type="text" v-model="numberValidateForm.code"></el-input>
        <el-button @click="sendEmail" style="float: right;">发送邮箱</el-button>
      </el-form-item>
      <el-form-item label="输入新密码" prop="password">
        <el-input type="password" placeholder="请输入新密码" v-model.number="numberValidateForm.password" ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repassword">
        <el-input type="password" placeholder="请确认新密码" v-model.number="numberValidateForm.repassword" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="height: 50px;"></div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        numberValidateForm: {
          email: '',
          code: '',
          password: '',
          repassword: ''
        }
      }
    },
    methods: {
      // 提交表单
      submitForm: function() {
        alert("tijia");

        if (this.numberValidateForm.code == localStorage.getItem('checkCode')) {
          if (this.numberValidateForm.password == this.numberValidateForm.repassword) {
            this.$http
              .put('user/backpassword?password='+this.numberValidateForm.password+"&email="+this.numberValidateForm.email)
              .then(res => {
                if (res.data.statusCode == 200) {
                  localStorage.removeItem('checkCode');
                  this.$message({
                    message: res.data.msg,
                    type: 'success'
                  });
                  this.$router.push('/login')
                } else if (res.data.statusCode == 404) {
                  this.$message.error(res.data.msg)
                  this.$router.push('/login')
                } else if (res.data.statusCode == 500) {
                  this.$message.error(res.data.msg)
                  this.$router.push('/login')
                }
              })
              .catch(error => {
                this.$message({
                  message: "修改成功，请登录",
                  type: 'success'
                });
                this.$router.push('/login')
              })
          } else {
            this.$message.error('两次密码不一致')
          }
        } else if (this.numberValidateForm.code == null || this.numberValidateForm.code == '') {
          this.$message.error('验证码为空')
        } else {
          this.$message.error('验证码错误')
        }
      },
      resetForm: function() {
        this.numberValidateForm.email = '';
        this.numberValidateForm.code = '';
        this.numberValidateForm.password = '';
        this.numberValidateForm.repassword = ''
      },
      /*发送邮件*/
      sendEmail: function() {
        if (this.numberValidateForm.email != null) {
          this.$http
            .post('log/sendEmail?email=' + this.numberValidateForm.email)
            .then(res => {
              if (res.data.statusCode == 200) {
                localStorage.setItem('checkCode', res.data.data);
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                })
              } else if (res.data.statusCode == 404) {
                this.$message.error(res.data.msg)
              }
            })
            .catch(error => {
              this.$message(error)
            })
        } else {
          this.$message({
            message: '邮箱不能为空',
            type: danger
          })
        }

      },
    }
  }
</script>
<style>
  #box{
    background-image: url("http://qsrr7y19m.hn-bkt.clouddn.com/tu2.jfif");
    text-align:center;   /*div盒子内的文字水平居中*/

    margin:0 auto;    /*div盒子水平居中*/
  }
  .registForm {
    margin: 10px auto;
    width: 400px;
    /*background: #fff;*/
    /*box-shadow: 0 0 35px #B4BCCC;*/
    padding: 60px 80px 30px 30px;
    border-radius: 25px;
    /*margin-top: 100px;*/
    background-color: transparent;
    font-weight:bold
  }
 /* .login-form {
    margin: 20px auto;
    width: 400px;
    !*background: #fff;*!
    !*box-shadow: 0 0 35px #B4BCCC;*!
    padding: 60px 80px 30px 30px;
    border-radius: 25px;
    margin-top: 100px;
    background-color: transparent;
    font-weight:bold

  }*/
  .registForm el-input {
    background-color: transparent;
    font-weight:bold
  }
</style>
